<?php
    /**
    * Mobile Connectors
    * Submit Meeting Request
    * 
    * @author trungdt
    * @since Dec 10, 2012
    * @copyright ABSoft Viet Nam
    * 
    * Input
    * Required
    * - session_id
    * - meeting_date: m/d/Y H:m:s
    * - meeting_duration: int
    * - meeting_place : text
    * - meeting_language : text
    * - meeting_type: text
    * - meeting_record : 0/1
    * - meeting_notes: text
    * - mentor_request : 0/1
    * - mentor_required : text
    * 
    * Output
    * {
    *   status : OK | NOT_AUTHENTICATE | NOT_VALID
    * }
    */

    global $db;
    // include the mobile connectors
    include_once(BASE."modules/profilemodule/mobile_connector.php");

    $ret = null; // init object
    $ret->status = "NOT_VALID";

    if (isset($_POST['session_id']))
    {
        $session_id = _ab_safe_html_string($_POST['session_id']);

        // check session is still valid or not
        // all timedout session had been wiped
        $msession = $db->selectObject("profilemodule_mobilesession", "session_id = '{$session_id}'");

        if (isset($msession->id))
        {
            $ret->status="OK";
            
            // session still valid, 
            profile_module_updateClientSession_last_response($session_id);
            
            // receive the information and validate
            $user_id = $msession->user_id;
            
            $muser = exponent_users_getUserById($user_id);
            
            $request=null;
            $request->user_id = $user_id;
            $request->last_update = time();
            $request->last_update_by = "{$muser->firstname} {$muser->lastname}({$muser->username})";
            $request->user_target = $muser->username;
            $request->meeting_date = strtotime($_POST['meeting_date']);
            
            // date could not translate to INT
            if ($request->meeting_date === false || $request->meeting_date ===-1)
                $ret->status="NOT_VALID";
            
            $request->meeting_duration = intval($_POST['meeting_duration']);
            $request->meeting_place = _ab_safe_html_string($_POST['meeting_place']);
            $request->meeting_language  = _ab_safe_html_string($_POST['meeting_language']);
            $request->meeting_type  = _ab_safe_html_string($_POST['meeting_type']);
            
            $request->meeting_record =0;
            if (isset($_POST['meeting_record']))
                $request->meeting_record = 1;
            
            $request->meeting_notes  = _ab_safe_html_string($_POST['meeting_notes']);
            
            $request->mentor_request = 0;
            if (isset($_POST['mentor_request']))
                $request->mentor_request = 1;
                
            $request->translator_request = 0;
            if (isset($_POST['translator_request']))
            $request->translator_request = 1;
            
            $request->mentor_required  = _ab_safe_html_string($_POST['mentor_required']);
            
            $request->request_date = time();
            $request->approve_date =0;
            $request->approve_by = "";
            $request->email = $muser->email;
            $request->approve_state = 0;
            $request->approve_note ="";
            
            if ($ret->status == "OK")
                // insert
                $db->insertObject($request, "profilemodule_meeting");
        }
        else
        {
            $ret->status="NOT_AUTHENTICATE";
        }
    }
    else
    {
        $ret->status="NOT_VALID";
    }

    echo json_encode($ret);
    die();
?>